const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    date_popup_show:false,
    root:app.domain,
    page:1,//页数
    list:[],//客户数组
    keyword:'',//搜索关键词
    belongOption:[],
    belongTag:'',//客户归属
    labelTag:'',
    labelOption:[],
    sort_show:false,
    sortOption: [],
    sort:'',
    filter_show:false,
    sourceOption:[],
    sourceTag:'',
    stateOption:[],
    stateTag:'',
  },

  edit(){
    wx.navigateTo({
      url: '../clientEdit/clientEdit',
    })
  },
  condition(){
    this.setData({
      filter_show:true,
    })
  },
  /**搜索文字变动 */
  searchChange(e){
    this.setData({
      keyword: e.detail,
    });
  },
  onSearch(){
    this.requestData();
  },
  /**点击待考试试题 */
  detail(e){
    wx.navigateTo({
      url: '../clientInfo/clientInfo?id='+e.currentTarget.dataset.id,
    })
  },
  /**底部tabar切换 */
  onChange(event){
    let that = this;
    let index = event.detail;
    that.setData({
      active: index,
    })
    this.requestData()
  },

  /**筛选 */
  chooseTag(e){
    let value = e.currentTarget.dataset.value;
    let type = e.currentTarget.dataset.type;
    switch(type){
      case 'workFlow':
        this.setData({
          stateTag:this.data.stateTag==value?'':value
        })
        break;
      case 'belong':
        this.setData({
          belongTag:this.data.belongTag==value?'':value
        })
      break;
      case 'source':
        this.setData({
          sourceTag:this.data.sourceTag==value?'':value
        })
      break;
      case 'label':
        this.setData({
          labelTag:this.data.labelTag==value?'':value
        })
      break;
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    api.curlPost('?cla=clientApp&fun=search').then((res)=>{
      that.setData({
        sortOption:res.data.orderBy,
        belongOption:res.data.belong,
        labelOption:res.data.tag,
        sourceOption:res.data.source,
        stateOption:res.data.workFlow
      })
    })
  },

  /**
   * 展开排序
   */
  showSortPopup(){
    this.setData({
      sort_show:true,
    })
  },
  /**关闭排序 */
  onSortClose(){
    this.setData({
      sort_show:false
    })
  },
  onCellClick(e){
   
    this.setData({
      sort:e.currentTarget.dataset.name,
      sort_show:false
    })
    this.requestData();
  },
  onRadioChange(event) {
  
    this.setData({
      sort: event.detail,
      sort_show:false
    });
    this.requestData();
  },
  cancelFilter(){
    this.setData({
      filter_show:false
    })
  },
  resetTage(){
    this.setData({
      labelTag:'',
      stateTag:'',
      sourceTag:'',
      belongTag:'',
    })
  },
  confirmFilter(){
    this.setData({
      startDate:'',
      endDate:'',
    })
    this.requestData();
    this.cancelFilter();
  },
  /**
   * 请求数据
   */
  requestData:function(page=1){
    let that = this;
    let  url = '?cla=clientAppClass&fun=list&page='+page
      let para = {
        wd:this.data.keyword,
        belong:this.data.belongTag,
        tag:this.data.labelTag,
        workFlow:this.data.stateTag,
        source:this.data.sourceTag,
        orderBy:this.data.sort
      }
      api.curlPost(url,para).then((res)=>{
        if(!api.isBlank(res.data.result) || page ==1){
          let arr = page==1?res.data.result:that.data.list.concat(res.data.result);
          that.setData({
            list:arr,
            page:Number(res.data.page.pageNow) +1
          })
        }
        if(page ==1){
          that.setData({
            sort:that.data.sort?that.data.sort:res.data.orderBy,
            belongTag:res.data.belong
          })
        }
        wx.stopPullDownRefresh({
          success: (res) => {},
        })
      })
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.requestData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestData(this.data.page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})